<?php

/*
 * MODULES API
 */

function ts_register_module($module_name , $menu_title , $menu_position = 0 , $window_title = ''){
    global $ts_modules;
    return $ts_modules->register_module($module_name , $menu_title , $menu_position , $window_title);
}

function ts_register_module_page($module_name , $page_name , $menu_title , $window_title = ''){
    global $ts_modules;
    return $ts_modules->register_module_page($module_name , $page_name , $menu_title , $window_title);
}

function ts_get_modules_menu($exclude = null , $echo = true){
    global $ts_modules;
    return $ts_modules->get_modules_menu($exclude , $echo);
}

function ts_get_module_menu($module_name , $echo = true){
    global $ts_modules;
    return $ts_modules->get_module_menu($module_name , $echo);
}


/*
 * DIRECTORIES API
 */

function ts_get_directories($dir){
    return ts_scandir($dir , true);
}

function ts_get_files($dir){
    return ts_scandir($dir , false);
}

function ts_scandir($dir , $exclude_files = false){
    $return = array();

    $files = scandir($dir);
    foreach($files as $file){
        if($file != '..' && $file != '.'){
            if($exclude_files){
                if(is_dir($dir . '/' . $file)){
                    $return[] = $file;
                }
            }else{
                $return[] = $file;
            }
        }
    }

    return $return;
}


/*
 * VARIABLES API
 */

function ts_set_defaults($defaults , &$args){
    foreach($defaults as $key => $value){
        if(!isset($args[$key])){
            $args[$key] = $value;
        }
    }
}

function ts_parse_variables($args){
    foreach($args as $k => $v){
        $GLOBALS[$k] = $v;
    }
}

function ts_unset_variables($args){
    foreach($args as $k => $v){
        unset($GLOBALS[$k]);
    }
}


/*
 * ARRAYS API
 */

function ts_sksort(&$array , $subkey = "id" , $sort_ascending = false){
    if(count($array)){
        $temp_array[key($array)] = array_shift($array);
    }

    foreach($array as $key => $val){
        $offset = 0;
        $found = false;

        foreach($temp_array as $tmp_key => $tmp_val){
            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])){
                $temp_array = array_merge((array) array_slice($temp_array , 0 , $offset) ,
                        array($key => $val) ,
                        array_slice($temp_array , $offset)
                );
                $found = true;
            }
            $offset++;
        }

        if(!$found){
            $temp_array = array_merge($temp_array , array($key => $val));
        }
    }

    if($sort_ascending){
        $array = array_reverse($temp_array);
    }else{
        $array = $temp_array;
    }
}

?>